package com.pyue.eam.regi.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pyue.eam.admin.api.entity.regi.CqCompanyEntity;
import com.pyue.eam.admin.api.except.ApiAsserts;
import com.pyue.eam.common.security.util.SecurityUtils;
import com.pyue.eam.regi.mapper.CqCompanyMapper;
import com.pyue.eam.regi.service.CqCompanyService;
import org.springframework.stereotype.Service;

/**
 * 公司表
 *
 * @date 2023-11-17 15:34:46
 */
@Service
public class CqCompanyServiceImpl extends ServiceImpl<CqCompanyMapper, CqCompanyEntity> implements CqCompanyService {
    @Override
    public Long updateCompany(CqCompanyEntity cqCompany) {
        ApiAsserts.isTrue(cqCompany.getCode() != null, "公司代码不能为空");

        Long id = cqCompany.getId();
        if (id == null) {
            CqCompanyEntity tenantCompanyInfo = baseMapper.getTenantCompanyInfo();
            if (tenantCompanyInfo == null || tenantCompanyInfo.getId() == null) {
                save(cqCompany);
            } else {
                updateById(cqCompany.setId(tenantCompanyInfo.getId()));
            }
        } else {
            updateById(cqCompany);
        }
        return cqCompany.getId();
    }

    @Override
    public CqCompanyEntity getCompanyInfo() {
        // 一个租户只有一个公司
        return baseMapper.getTenantCompanyInfo();
    }

}
